<?php
declare(strict_types=1);

namespace App\__NameSpace__;

use App\Controller;
use Field\__Table__Const;
use Field\__Table__Columns;
use Field\__Table__Row;

// hook controller__class_name__use.php

/**
 * @title   $title$
 * @auth    $auth$
 * @login   $login$
 * @menu    $menu$
 * @component   $component$
 * @icon    $icon$
 * @active  true
 * @affix   false
 * @rank    $rank$
 */
class __CLASSNAME__ extends Controller
{
    // hook controller__class_name__public_start.php



    // hook controller__class_name__public_end.php

    /**
     * @title  下发配置
     * @rank    0
     */
    public function action_Conf_PUT()
    {
        // hook controller__class_name__conf_start.php
        $conf=$conf$;
        $Columns = $columns$;
        $Filter = $filter$;
        $Tools = $tools$;

        // hook controller__class_name__conf_end.php
        $this->response('0000', ['Conf' =>$conf, 'Filter' => $Filter, 'Tools' =>$Tools, 'Columns' => $Columns]);
    }

    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller__class_name__index_start.php
        $page = $this->request->param('page', 0);
        $size = $this->request->param('limit', 0);
        $field = $this->request->param('field', '$pk$');
        $sort = $this->request->param('sort', -1);
        $order = [];
        !empty($field) && $order[$field] = $sort == 'asc' ? -1 : 1;
        $cond = [];
        $filter_foreach$
        $data = $this->__Table__->GetList($cond, $order, $page, $size);
        $column_foreach_with$
        foreach ($data['data'] as &$row) {
            $column_foreach_with_fmt$
            //unset($row['PassWord'],$row['Salt']);
        }

        // hook controller__class_name__index_end.php
        $this->response('0000', $data);
    }
    //need_add_start
    /**
     * @title   新增配置
     * @rank    72
     */
    public function action_Add_PUT()
    {
        // hook controller__class_name__add_put_start.php
        $AddForm = $addform$;
        // hook controller__class_name__add_put_end.php
        $this->response('0000', ['data' => $AddForm]);
    }

    /**
     * @title   新增提交
     * @rank    71
     */
    public function action_Add_POST()
    {
        // hook controller__class_name__add_post_start.php
        $data = [];
        $add_foreach$
        $r = $this->__Table__->insert($data);
        // make addformpost __Table__ __NameSpace__
        // hook controller__class_name__add_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    //need_add_end
    //need_edit_start
    /**
     * @title   编辑配置
     * @rank    63
     */
    public function action_Edit_PUT()
    {
        // hook controller__class_name__edit_put_start.php
        $EditForm = $editform$;
        // hook controller__class_name__edit_put_end.php
        $this->response('0000', ['data' => $EditForm]);
    }

    /**
     * @title   编辑数据
     * @rank    62
     */
    public function action_Edit_GET()
    {
        // hook controller__class_name__edit_put_start.php
        $cond = [];
        $index_foreach$
        $this->CheckEmpty($cond, ['待修改数据']);
        $data = $this->__Table__->read($cond);
        // hook controller__class_name__edit_put_end.php
        $this->response('0000', ['data' => $data]);
    }

    /**
     * @title   编辑保存
     * @rank    61
     */
    public function action_Edit_POST()
    {
        // hook controller__class_name__edit_start.php
        $cond = [];
        $index_foreach$
        $this->CheckEmpty($cond, ['待修改数据']);
        $data = [];
        $edit_foreach$
        $r = $this->__Table__->update($cond, $data);
        // make editformpost __Table__ __NameSpace__
        // hook controller__class_name__edit_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    //need_edit_end
    //need_field_start
    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller__class_name__field_post_start.php
        $cond = [];
        $index_foreach$
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field,...$cond], ['待修改数据','待修改字段']);
        $field_fields = $field_fields$;
        $tmp_field=rtrim($field,'+-*/=');
        !in_array($tmp_field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($tmp_field, __Table__Const::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($tmp_field) {
        $column_field_switch_fmt$
        }
        $r = !empty($update) ? $this->__Table__->update($cond, $update) : false;
        // hook controller__class_name__field_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    //need_field_end
    //need_update_start
    /**
     * @title   批量编辑字段
     * @rank    60
     */
    public function action_Update_POST()
    {
        // hook controller__class_name__update_post_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
            $index_foreach_kv$
        }
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field,...$cond], ['待修改数据','字段']);
        $update_fields = $field_batch_fields$;
        $tmp_field=rtrim($field,'+-*/=');
        !in_array($tmp_field, $update_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($tmp_field, __Table__Const::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($tmp_field) {
        $column_batch_field_switch_fmt$
        }
        $r = !empty($update) ? $this->__Table__->update($cond, $update) : false;
        // hook controller__class_name__update_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    //need_update_end

    //need_status_start
    /**
     * @title   启用
     * @rank    41
     */
    public function action_Enable_POST()
    {
        // hook controller__class_name__enable_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
                $index_foreach_kv$
        }
        $this->CheckEmpty($cond, ['待修改数据']);
        $r = $this->__Table__->update($cond, [__Table__Const::Status => 1]);
        // hook controller__class_name__enable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }

    /**
     * @title   禁用
     * @rank    40
     */
    public function action_Disable_POST()
    {
        // hook controller__class_name__disable_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
                $index_foreach_kv$
        }
        $this->CheckEmpty($cond, ['操作数据为空']);
        $r = $this->__Table__->update($cond, [__Table__Const::Status => 0]);
        // hook controller__class_name__disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r],$r?'操作成功':'操作失败');
    }
    //need_status_end
    //need_option_start
    /**
     * @title   下拉选择属性
     * @rank    30
     */
    public function action_Option_GET()
    {
        // hook controller__class_name__option_start.php

        $value = $this->request->param('value');
        $cond = [
            'OptionField'=>['LIKE'=>$value]
        ];
        $order = [];
        $list = $this->__Table__->select($cond, $order, 1, 100);
        $data=[];
        foreach ($list as $row){
            $data[]=[
              'label'=>$row['OptionLabel'],
              'value'=>$row['OptionValue'],
            ];
        }
        // hook controller__class_name__option_end.php
        $this->response('0000', ['data' => $data]);
    }
    //need_option_end
    //need_import_start
   /**
    * @title 导入
    * @rank    20
    */
    public function action_Import_POST()
    {
        // hook controller__class_name__import_start.php
        $files = $this->request->getFiles();
        empty($files['file']) && $this->response('0003',[],'文件为空');
        $config   = ['path' =>__WEBDIR__];
        $excel    = new \Vtiful\Kernel\Excel($config);
        // 导出测试文件
        //$filePath = $excel->fileName('tutorial.xlsx')->header(['Item', 'Cost'])->output();

        // 读取测试文件
        $excel->openFile($files['file']['tmp_name'])->openSheet();
        // 此处判断请使用【!==】运算符进行判断；
        // 如果使用【!=】进行判断，出现空行时，返回空数组，将导致读取中断；
        while (($row = $excel->nextRow()) !== NULL) {
            var_dump($row);
        }
        // hook controller__class_name__import_end.php
        $this->response('0000', ['data' => []]);
    }
    //need_import_end
    //need_export_start
    /**
     * @title 导出
     * @rank    10
     */
    public function action_Export_POST()
    {
        // hook controller__class_name__export_start.php
        $page = $this->request->param('page', 0);
        $size = $this->request->param('limit', 0);
        $field = $this->request->param('field', '');
        $sort = $this->request->param('sort', '');
        $order = [];
        !empty($field) && $order[$field] = $sort == 'asc' ? -1 : 1;
        $cond = [];
        $filter_foreach$
        $data = $this->__Table__->GetList($cond, $order, $page, $size);
        $column_foreach_with$
        foreach ($data['data'] as &$row) {
            $column_foreach_with_fmt$
        }

        $config = [
            'path' =>__WEBDIR__// xlsx文件保存路径
        ];
        $excel  = new \Vtiful\Kernel\Excel($config);
        $name = date("YmdHis").'$title$数据导出.xlsx';
        // fileName 会自动创建一个工作表，你可以自定义该工作表名称，工作表名称为可选参数
        $filename = $excel->fileName($name, 'sheet1')
            ->header(['Item', 'Cost'])
            ->data([
                ['Rent', 1000],
                ['Gas',  100],
                ['Food', 300],
                ['Gym',  50],
            ])->output();
        // hook controller__class_name__export_end.php
        $this->response('0000', ['ext' => 'xlsx', 'name' =>$name, 'filename' => $filename], '导出成功', '', 1001);
    }
    //need_export_end

      //need_delete_start
     /**
      * @title   删除数据
      * @rank    50
      */
    public function action_Index_Delete()
    {
        // hook controller__class_name__delete_start.php
        $data = $this->request->param('data', []);
        $cond=[];
        foreach ($data as $row){
            $index_foreach_kv$
        }
        $this->CheckEmpty($cond, ['操作数据为空']);
        $r = $this->__Table__->delete($cond);
        // hook controller__class_name__delete_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }
    //need_delete_end
    // hook controller__class_name__end.php
}

?>